package com.itita.ww2.game.alliance;

import com.itita.ww2.puredb.model.Alliance;
import com.itita.ww2.redis.RedisUtil;
import com.itita.ww2.redis.cache.Table;
import com.itita.ww2.utils.DataUtil;

public class AllianceWMDataManager {
	private Alliance alliance;
	private Table table;
	private String[] redisKey;

	public void init(Alliance alliance) {
		this.alliance = alliance;
		String aid = this.alliance.getAid();
		this.table = AllianceManager.getInstance().getCacheAlWorldMapTable();
		this.redisKey = RedisUtil.genericKey(aid);
	}

	/**
	 * 联盟数据操作<br>
	 * base_occTimes/base_defTimes/city_occTimes/city_defTimes/base_count/city_count
	 */
	private void dataCountOper(String field, int value) {
		int temp = DataUtil.intValueOf(table.getValue(redisKey, field));
		if (temp == -1) {
			temp = 0;
		}
		temp += value;
		temp = temp < 0 ? 0 : temp;
		table.setValue(redisKey, field, temp);
	}

	public void occBase() {
		dataCountOper("base_occTimes", 1);
		dataCountOper("base_count", 1);
	}

	public void lostBase() {
		dataCountOper("base_count", -1);
	}

	public void defendBase() {
		dataCountOper("base_defTimes", 1);
	}

	public void occCity() {
		dataCountOper("city_occTimes", 1);
		dataCountOper("city_count", 1);
	}

	public void lostCity() {
		dataCountOper("city_count", -1);
	}

	public void defendCity() {
		dataCountOper("city_defTimes", 1);
	}
}
